package org.abacus.client.ui.view.login;

import org.abacus.client.ui.view.DialogView;
import org.abacus.model.sys.Role;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;

import request.ActionReturn;

public class RoleView extends DialogView {

	private static String roleId;

	private Role[] roles;
	List list;

	public Role[] getRoles() {
		return roles;
	}

	public void setRoles(Role[] roles) {
		this.roles = roles;
		if (null != this.roles) {
			String[] items = new String[roles.length];
			int i = 0;
			for (Role role : roles) {
				items[i] = role.getName();
				i++;
			}
			list.removeAll();
			list.setItems(items);
		}
	}

	public static String getRoleId() {
		return roleId;
	}

	Group group;

	@Override
	protected void initView() {
		super.initView();
		GridLayout gridLayout = null;

		shell.setText("选择角色");
		group = new Group(shell, SWT.NONE);
		{
			gridLayout = new GridLayout();
			gridLayout.numColumns = 1;
			gridLayout.makeColumnsEqualWidth = false;

			gridLayout.verticalSpacing = 20;

			group.setLayout(gridLayout);
			group.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		}
		group.setText("角色列表");
		list = new List(group, SWT.BORDER);

		list.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, true));
		group.pack();
		shell.pack();
		Button button = null;

		Composite composite = new Composite(shell, SWT.NONE);
		{
			gridLayout = new GridLayout();
			gridLayout.numColumns = 2;
			gridLayout.makeColumnsEqualWidth = true;
			composite.setLayout(gridLayout);
			composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
		}
		button = new Button(composite, SWT.PUSH);
		button.setText("取消");
		button.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		button.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(Event arg0) {
				cancel();
			}
		});

		ok = button = new Button(composite, SWT.PUSH);
		button.setText("确定");
		button.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
		button.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(Event arg0) {
				ok();
			}
		});
		centerShell(shell, 350, 260);
		shell.pack();
		shell.open();
	}

	private Button ok;

	@Override
	public void ok() {
		ok.setEnabled(false);
		try {
			roleId = ((Text) this.scrollables.get("roleId")).getText();
			ActionReturn<Boolean> done = RoleView.this.getActionRequest().done(getEntrySetScrollables(), Boolean.class);
			if (null != done && null != done.getResult() && done.getResult().booleanValue()) {
				if (getActionRequest().hasNext()) {
					getActionRequest().next().done(null, null);
				}
			} else {
				ok.setEnabled(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void cancel() {
		ok.setEnabled(true);
	}

	@Override
	public void dispose() {
		getShell().dispose();
	}

}
